import { ModelInitState } from '../utils/constant';
import { findByPage as findUserByPage } from '../services/manage-by-user-controller';
import { findByPage as findRoleByPage } from '../services/manage-by-role-controller';
import { findByPage as findPermissionByPage } from '../services/manage-by-permission-controller';
import { findRoleNameByUser, findPermissionStrByRole } from '../services/manage-by-query-controller';
import { singleUserBindRole, singleUserUnBindRole, singleRoleBindPermission, singleRoleUnBindPermission } from '../services/manage-by-security-controller';

export default {
  namespace: 'AuthorizationModel',

  state: {
    // 用户列表
    queryUserParam: {
      ...ModelInitState.queryParam,
      pageSize: 50,
      search: undefined,
    },
    userTablePagination: {
      ...ModelInitState.pagination,
      simple: true,
      // showSizeChanger: true,
      // showQuickJumper: false,
      // showTotal: false,
    },
    userTableData: [],
    selectUser: undefined,

    // 角色列表
    queryRoleParam: {
      ...ModelInitState.queryParam,
      pageSize: 50,
      name: undefined,
    },
    roleTablePagination: {
      ...ModelInitState.pagination,
      simple: true,
    },
    roleTableData: [],
    selectRoleList: [],
    selectRole: undefined,

    // 权限列表
    queryPermissionParam: {
      ...ModelInitState.queryParam,
      pageSize: 50,
      sysName: undefined,
      resourcesType: undefined,
      needAuthorization: '1',
      title: undefined,
      permissionStr: undefined,
    },
    permissionTablePagination: {
      ...ModelInitState.pagination,
      // simple: true,
    },
    permissionTableData: [],
    selectPermissionList: [],
  },

  effects: {
    *findUserByPage({ payload }, { select, call, put }) {
      let queryUserParam = yield select(state => state.AuthorizationModel.queryUserParam);
      let userTablePagination = yield select(state => state.AuthorizationModel.userTablePagination);
      queryUserParam = { ...queryUserParam, ...payload }
      // 请求数据
      const resultData = yield call(findUserByPage, queryUserParam);
      if (!resultData) return;
      const { records, total, size, current } = resultData;
      if (!records) return;
      // 保存数据
      userTablePagination = { ...userTablePagination, pageSize: size, current, total };
      yield put({ type: 'save', payload: { queryUserParam, userTablePagination, userTableData: records } });
    },
    *findRoleByPage({ payload }, { select, call, put }) {
      let queryRoleParam = yield select(state => state.AuthorizationModel.queryRoleParam);
      let roleTablePagination = yield select(state => state.AuthorizationModel.roleTablePagination);
      queryRoleParam = { ...queryRoleParam, ...payload }
      // 请求数据
      const resultData = yield call(findRoleByPage, queryRoleParam);
      if (!resultData) return;
      const { records, total, size, current } = resultData;
      if (!records) return;
      // 保存数据
      roleTablePagination = { ...roleTablePagination, pageSize: size, current, total };
      yield put({ type: 'save', payload: { queryRoleParam, roleTablePagination, roleTableData: records } });
    },
    *findPermissionByPage({ payload }, { select, call, put }) {
      let queryPermissionParam = yield select(state => state.AuthorizationModel.queryPermissionParam);
      let permissionTablePagination = yield select(state => state.AuthorizationModel.permissionTablePagination);
      queryPermissionParam = { ...queryPermissionParam, ...payload }
      // 请求数据
      const resultData = yield call(findPermissionByPage, queryPermissionParam);
      if (!resultData) return;
      const { records, total, size, current } = resultData;
      if (!records) return;
      // 保存数据
      permissionTablePagination = { ...permissionTablePagination, pageSize: size, current, total };
      yield put({ type: 'save', payload: { permissionTableData: records, queryPermissionParam, permissionTablePagination } });
    },
    *findRoleNameByUser({ payload }, { call, put }) {
      const { username } = payload;
      // 请求数据
      const result = yield call(findRoleNameByUser, username);
      if (!result) return;
      yield put({ type: 'save', payload: { selectRoleList: result } });
    },
    *findPermissionStrByRole({ payload }, { call, put }) {
      const { roleName } = payload;
      // 请求数据
      const result = yield call(findPermissionStrByRole, roleName);
      if (!result) return;
      yield put({ type: 'save', payload: { selectPermissionList: result } });
    },
    *singleUserBindRole({ payload, successCallBack }, { call, put }) {
      const { username, roleName } = payload;
      // 请求数据
      const result = yield call(singleUserBindRole, { username, roleName });
      if (!result) return;
      // 刷新数据
      yield put({ type: 'save', payload: { selectRoleList: result.roleList.map(item => item.name) } });
      // yield put({ type: 'findRoleNameByUser', payload: { username } });
      if (successCallBack instanceof Function) successCallBack(result);
    },
    *singleUserUnBindRole({ payload, successCallBack }, { call, put }) {
      const { username, roleName } = payload;
      // 请求数据
      const result = yield call(singleUserUnBindRole, { username, roleName });
      if (!result) return;
      // 刷新数据
      yield put({ type: 'save', payload: { selectRoleList: result.roleList.map(item => item.name) } });
      // yield put({ type: 'findRoleNameByUser', payload: { username } });
      if (successCallBack instanceof Function) successCallBack(result);
    },
    *singleRoleBindPermission({ payload, successCallBack }, { call, put }) {
      const { roleName, permissionStr } = payload;
      // 请求数据
      const result = yield call(singleRoleBindPermission, { roleName, permissionStr });
      if (!result) return;
      // 刷新数据
      yield put({ type: 'save', payload: { selectPermissionList: result.permissionList.map(item => item.permissionStr) } });
      // yield put({ type: 'findPermissionStrByRole', payload: { roleName } });
      if (successCallBack instanceof Function) successCallBack(result);
    },
    *singleRoleUnBindPermission({ payload, successCallBack }, { call, put }) {
      const { roleName, permissionStr } = payload;
      // 请求数据
      const result = yield call(singleRoleUnBindPermission, { roleName, permissionStr });
      if (!result) return;
      // 刷新数据
      yield put({ type: 'save', payload: { selectPermissionList: result.permissionList.map(item => item.permissionStr) } });
      // yield put({ type: 'findPermissionStrByRole', payload: { roleName } });
      if (successCallBack instanceof Function) successCallBack(result);
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
